package com.rongshu.view.penpal.present;

import android.Manifest;
import android.content.Context;

import com.rongshu.api.RetorfitService;
import com.rongshu.utils.Common;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.home.entity.ShareInformation;
import com.rongshu.view.penpal.bean.PenpalMessageResponse;
import com.rongshu.view.penpal.view.PenpalDetailsView;
import com.widget.toast.ToastUtils;

import java.util.HashMap;
import java.util.Map;

import rx.Subscriber;
import rx.functions.Action0;

/**
 * created by zhengweis on 2018/11/21.
 * description：
 */
public class PenpalDetailsPresenter extends BasePresenter<PenpalDetailsView> {
    private ShareInformation.ResponseBean response;
    String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.READ_EXTERNAL_STORAGE};

    public PenpalDetailsPresenter(Context context) {
        super(context);
    }

    public void getPenpalDetails(int penpalwill_id) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("penpalwill_id", penpalwill_id);
        RetorfitService.getPenpalMessageById(UserInfoUtils.getUserId(), penpalwill_id,
                Common.encryptionSign(map))
                .compose(getView().<PenpalMessageResponse>bindToLife()).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<PenpalMessageResponse>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(PenpalMessageResponse object) {
                getView().callBackPenpalMsg(object);
            }
        });
    }

    public void requestAddPenpal(int penpalwill_id) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("penpalwill_id", penpalwill_id);
        RetorfitService.requestAddPenpal(UserInfoUtils.getUserId(), penpalwill_id,
                Common.encryptionSign(map))
                .compose(getView().<INetBean>bindToLife()).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<INetBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(INetBean object) {
                ToastUtils.show(object.getErrMsg());
                if (object.getErrCode() == 0) {
                    getView().callBackRequsetJoinResult();
                }
            }
        });
    }

    public void getShareUrl() {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        RetorfitService.getShareUrl(UserInfoUtils.getUserId(),
                Common.encryptionSign(map))
                .compose(getView().<ShareInformation>bindToLife()).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<ShareInformation>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(ShareInformation object) {
                response = object.getResponse();
                if (object.getErrCode() == 0) {
                    getView().callBackShareResponse(object);
                } else {
                    ToastUtils.show(object.getErrMsg());
                }
            }
        });
    }
}
